﻿using System;
using System.Collections.Generic;
using Legazpi.Payroll.Services.Interfaces;
using Emz.Core.Repositories;
using System.Data.Entity;
using Emz.Core.Models;
using Emz.Core.Base;

namespace Legazpi.Payroll.Services.Services
{
   public class ClientService :IClientService
    {
        #region Fields

        private DbContext _dbContext;
        private IClientRepository _clientRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public ClientService()
            : this(new DatabaseFactory())
        {

        }

        public ClientService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _clientRepository = new ClientRepository(_dbContext);
            }
        }

        #endregion

        public Client GetById(int id)
        {
            return _clientRepository.GetById(id);
        }

        public IEnumerable<Client> GetAll()
        {
            return _clientRepository.GetAll();
        }

        public IEnumerable<Client> Get(int multiplier, int count)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Client> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(Client client)
        {        
            _clientRepository.Add(client);
        }

        public void Update(Client client)
        {         
            _clientRepository.Update(client);
        }

        public void Delete(int id)
        {
            var client = GetById(id);
            if (client != null)
                _clientRepository.Delete(client);
        }

        public bool Save()
        {
            string message;
            _clientRepository.Save(out message);
            return message == "Saved";
        }
    }
}
